<?php
/**
 * Created by VISPHP 快速开发框架.
 * Author:乐清
 * email:804966813@qq.com
 * Date: 2019/2/17
 * Time: 10:34
 */

namespace app\admin\controller;

use app\admin\model\Email as mail;

class Email extends Visphp
{
    public function index(){
        $db=new mail();
        $data=$db->where('email','email')->find();
        $this->assign('data',$data);
        return view('/sys/email');
    }
    public function edit(){
        if (request()->isAjax()){
            $db=new mail();
            $post = $this->request->post();
            //验证  唯一规则： 表名，字段名，排除主键值，主键名
            $validate = new \think\Validate([
                ['from_email', 'require|email', '发件箱不能为空|发件箱格式不正确'],
                ['from_name', 'require', '发件人不能为空'],
                ['smtp', 'require', '邮箱smtp服务器不能为空'],
                ['username', 'require|email', '登录账户不能为空|登录账户应为邮箱格式'],
                ['password', 'require', '登录密码不能为空'],
                ['content', 'require', '邮件模板不能为空'],
            ]);
            //验证部分数据合法性
            if (!$validate->check($post)) {
                $this->error('提交失败：' . $validate->getError());
            }

            if(false == $db->save($post,['email'=>'email'])) {
                 $this->error('提交失败');
            } else {
                 $this->success('提交成功','admin/email/index');
            }
        }else{
            $this->error('非法请求');
        }
    }
    public function sendto(){
        if($this->request->isAjax()) {
            $post = $this->request->post();
            //验证  唯一规则： 表名，字段名，排除主键值，主键名
            $validate = new \think\Validate([
                ['email', 'require|email', '收件箱不能为空|收件箱格式不正确'],
            ]);
            //验证部分数据合法性
            if (!$validate->check($post)) {
                $this->error('提交失败：' . $validate->getError());
            }

            $address = $post['email'];
            $mailto = SendMail($address);
            if(false == $mailto) {
                $this->error('发送失败');
            } else {

                $this->success('邮件发送成功');
            }

        }
    }
}